const DS = require('./DataStruct')
const DC = require('./DataCenter')
roomMemberTrans = (room) => {
    const temp = new Object()
    temp.roomId = room.id
    temp.roomName = room.name
    temp.member = []

    room.member.forEach(member => {
        const tempMember = new Object()
        tempMember.id = member.id
        tempMember.name = member.name
        tempMember.isReady = member.connect.isReady
        temp.member.push(tempMember)
    })
    return temp
}
roomListTrans = () => {
    const msg = []
    DC.RoomList.forEach(room => {
        const temp = roomMemberTrans(room)
        msg.push(temp)
    })
    return msg
}
module.exports = {
    //创建房间
    Create: (connect) => {
        if (connect.roomId) {
            let msg = new Object()
            msg.code = 200
            msg.msg = '创建失败，请勿重复创建'
            let data = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.CREATE, msg)
            connect.sendText(data)
        } else {
            //TODO:1.创建玩家对象
            var player = new Object()
            player.id = connect.id
            player.name = connect.name
            player.isReady = false
            player.connect = connect
            //TODO:2.创建房间对象
            var room = new Object()
            room.id = Date.now()
            connect.roomId = room.id
            room.name = connect.name + '的房间'
            //TODO:3.将玩家对象放进房间对象数组中
            room.member = []
            room.member.push(player)
            //TODO:4.将房间对象放进房间列表中
            DC.RoomList.push(room)
            const msg = roomMemberTrans(room)
            msg.code = 200
            msg.msg = '创建成功'
            let data = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.CREATE, msg)
            connect.sendText(data)
        }
    },
    List: (connect) => {
        //因为DC.RoomList存在connect连接，数据量过大
        //不能发送，所以需要重构一下信息
        const msg = new Object()
        msg.code = 200
        msg.msg = '获取房间列表成功'
        msg.list = roomListTrans()
        let data = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.LIST, msg)
        connect.sendText(data)
    },
    //加入房间
    Join: (data, connect) => {
        if (connect.roomId) {
            let msg = new Object()
            msg.code = 200
            msg.msg = '加入失败，请勿重复加入'
            let data = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.CREATE, msg)
            connect.sendText(data)
        } else {
            //TODO:1.遍历房间列表（DC.RoomList）
            //找到id相同的房间
            let flag=false;
            DC.RoomList.forEach(room => {
                if (room.id == data.id) {
                    //TODO:2.将此玩家加入到该房间
                    let curConn = new Object()
                    curConn.id = connect.id
                    curConn.name = connect.name
                    connect.roomId = room.id
                    curConn.isReady = connect.isReady
                    curConn.connect = connect
                    room.member.push(curConn)
                    //TODO:3.将此房间的信息返回给客户端
                    const roomTrans = roomMemberTrans(room)
                    let msg = new Object()
                    msg.code = 200
                    msg.msg = connect.name + '加入成功'
                    msg.member = roomTrans
                    let data = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.JOIN, msg)
                    //TODO:4.房间信息广播给房间内所有人
                    DC.BoardCast(true, data, room.member)
                    flag=true
                }
            })
            if(!flag)
            {   
                let msg = new Object()
                msg.code = 200
                msg.msg = '加入失败，房间号错误或者房间已解散'
                let pack_data = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.CREATE, msg)
                connect.sendText(pack_data)
            }

        }
    },
    //退出房间
    Quit: (connect) => {
        //TODO:1.找到指定id的房间
        DC.RoomList.forEach((room, index) => {
            if (room.id === connect.roomId) {
                //TODO:2.找到指定id的成员                
                room.member.forEach((member, index1) => {
                    if (member.id === connect.id) {
                        connect.endTime = null
                        connect.roomId = null
                        connect.isReady = false
                        //TODO:3.将当前连接从房间数组中删除
                        room.member.splice(index1, 1)
                        //如果房间里没有成员，销毁房间
                        if (room.member.length === 0) {
                            DC.RoomList.splice(index, 1)
                            let msg = new Object()
                            msg.code = 200
                            msg.msg = '退出房间成功，房间已解散'
                            let sendMsg = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.QUIT, msg)
                            return connect.sendText(sendMsg)
                        }
                        //私发消息给个人退出房间成功
                        let msg = new Object()
                        msg.code = 200
                        msg.msg = '退出房间成功'
                        let sendMsg = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.QUIT, msg)
                        connect.sendText(sendMsg)
                        //将此消息广播给房间内所有人
                        let allMsg = new Object()
                        allMsg.code = 200
                        allMsg.msg = connect.name + '退出房间'
                        allMsg.list = roomMemberTrans(room)
                        let allSendMsg = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.QUIT, allMsg)
                        DC.BoardCast(true, allSendMsg, room.member)
                    }
                })
            }
        })
    },
    //准备
    Ready: (connect) => {
        let msg = new Object()
        if (connect.roomId) {
            if (connect.isReady) {
                msg.code = 200
                msg.msg = '您已经准备了，请勿重复准备'
                let sendMsg = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.Ready, msg)
                connect.sendText(sendMsg)
                return
            }
            connect.isReady = true
            DC.RoomList.forEach(room => {
                if (room.id === connect.roomId) {
                    room.member.forEach(member => {
                        if (member.id === connect.id) {
                            member.isReady = connect.isReady
                            msg.code = 200
                            msg.msg = connect.name + '准备成功'
                            msg.list = roomMemberTrans(room)
                            let sendMsg = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.READY, msg)
                            console.log(sendMsg)
                            DC.BoardCast(true, sendMsg, room.member)
                        }
                    })
                }
            })
        } else {
            msg.code = 200
            msg.msg = '您还未加入房间'
            let sendMsg = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.Ready, msg)
            connect.sendText(sendMsg)
        }
    },
    //取消准备
    Unready: (connect) => {
        let msg = new Object()
        if (connect.roomId) {
            if (!connect.isReady) {
                msg.code = 200
                msg.msg = '您已经处于未准备状态'
                let sendMsg = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.UNREADY, msg)
                connect.sendText(sendMsg)
                return
            }
            connect.isReady = false
            DC.RoomList.forEach(room => {
                if (room.id === connect.roomId) {
                    room.member.forEach(member => {
                        if (member.id === connect.id) {
                            member.isReady = connect.isReady
                            msg.code = 200
                            msg.msg = connect.name + '取消准备成功'
                            msg.list = roomMemberTrans(room)
                            let sendMsg = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.UNREADY, msg)
                            DC.BoardCast(true, sendMsg, room.member)
                        }
                    })
                }
            })
        } else {
            msg.code = 200
            msg.msg = '您还未加入房间'
            let sendMsg = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.UNREADY, msg)
            connect.sendText(sendMsg)
        }
    },
    //开始游戏
    //1.所有人都准备才可以开始游戏
    //2.开始游戏时先进入倒计时，倒计时结束正式开始
    //计时器
    // setInterval(() => {
    // }, interval)
    //清除计时器
    // clearIntervalcl()
    Start: (connect) => {
        let msg = new Object()
        let unReadyMember = 0
        DC.RoomList.forEach(room => {
            if (room.id === connect.roomId) {
                console.log(room.member)
                room.member.forEach(member => {
                    if (member.isReady === false) {
                        unReadyMember++
                    }
                })
                if (unReadyMember > 0) {
                    msg.code = 200
                    msg.msg = '还有人未准备'
                    let sendMsg = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.START, msg)
                    DC.BoardCast(true, sendMsg, room.member)
                } else {
                    let i = 3
                    let timer = setInterval(() => {
                        msg.code = 200
                        msg.msg = i
                        let sendMsg = DS.packData(DS.RequestCode.ROOM, DS.RoomAction.START, msg)
                        DC.BoardCast(true, sendMsg, room.member)
                        i--
                        if (i <= 0) {
                            clearInterval(timer)
                            //到达终点成员数量
                            room.overMember = 0
                        }                        
                    }, 1000);
                }
            }
        })
    }
}